package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.LogService;
import com.zzr.demo.redis.service.ShortURLService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description:
 */
@RestController
@RequestMapping("short/url")
@Api(tags = "短地址：为了减少微博的文字")
@Slf4j
public class ShortURLController {

    @Autowired
    ShortURLService shortURLService;

    @PostMapping("/transfer")
    @ApiOperation("长地址转换")
    public String transfer(String url) {
        String transfer = shortURLService.transfer(url);
        return transfer;
    }

    /**
     * eg.可以通过访问：R.cn/2 来访问之前存储的值
     *
     * @param url
     * @return
     */
    @PostMapping("/getRaw")
    @ApiOperation("根据短地址获取源长地址")
    public String getRaw(String url) {
        String id = url.substring(url.lastIndexOf("/") + 1);
        String raw = shortURLService.getRaw(id);
        return raw;
    }


}
